<?php
/**
 * Created by PhpStorm.
 * User: Apache
 *  展商  展品管理
 *
 */

namespace app\api\controller\activemanage;
use app\api\controller\common\ApiBase;
use think\Db;
use think\Model;
use app\api\model\Admin as AdminModel;
use app\api\controller\common\CommonPlus;
use think\Controller; // dengh quxiao
// Controller  ApiBase （dengl）
class Activequdao extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
        //$this->model = model('CompanyPublic');
    }

    public function getTabName($exhibition_id){
        $result=db('exhibition')->where(['id'=>$exhibition_id])->find();
        $tabName='';
        if($result){
            $tabName=$result['create_person_table'];
        }else{
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        return $tabName;
    }


    /**
     *
     * @return \think\response\Json|void
     *  comppub
     *
     */

    public function getList(){
        $page = $this->request->param('page');
        $page = max(1,intval($page));
        $pagesize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
        $exhibition_id =$this->request->param('exhibition_id');

        $param=" and w.is_delete=0 ";
        if(!empty($exhibition_id)){
            $param.=" and w.exhibition_id=$exhibition_id ";
        }
        $keyword =input('keyword');
        if (!empty($keyword)) {
            $param.=" and w.title like '%$keyword%' ";
        }

        $sql2="select w.*,c.id as exhibition_id,c.title as exhibition_title,p.person_all";
        $sql2.=" from crm_active_zsqudao as w LEFT JOIN crm_exhibition as c ON w.exhibition_id=c.id ";
        $sql2.=" LEFT JOIN (SELECT count(id) as person_all,channel_id FROM crm_zh_active_signup GROUP BY channel_id) AS p ON p.channel_id=w.id ";
        $sql2.=" where 1=1 $param order by w.id desc ";
        $total=Db::query($sql2);
        $sql2.=" limit ".($page-1)*$pagesize.",$pagesize";
        $list=Db::query($sql2);
        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'total'=>count($total)]]);
    }


    /**
     * 添加
     * @return \think\response\Json|void
     */
    public function add(){
        $com =new CommonPlus();
        $data['exhibition_id'] = $this->request->param('exhibition_id');
        $data['type']=$this->request->param('type');   // 中文
        $data['number']=$this->request->param('number');   // 中文
        $data['position']=$this->request->param('position');   // 中文
        $data['settlement']=$this->request->param('settlement');   // 中文
        $data['start_time']=$this->request->param('start_time');   // 中文
        $data['title']=$this->request->param('title');   // 中文
        $data['create_time']=time();   // 中文
        $data['update_time']=time();   // 中文
        $exhibition=db('exhibition')->where(['id'=>$data['exhibition_id']])->find();
        if(empty($exhibition)){
            return json(['code' => 0, 'msg' => '展会信息错误']);
        }
        if(empty($data['title'])){
            return json(['code'=>0,'msg'=>'渠道名称有误','data'=>'']);
        }
//        $qdInfo=db('active_zsqudao')->where(['title'=>$data['title'],'exhibition_id'=>$data['exhibition_id']])->find();
//        if($qdInfo){
//            return json(['code'=>0,'msg'=>'渠道名称重复','data'=>'']);
//        }
        if(!empty($data['start_time'])){
            $data['start_time']=strtotime($data['start_time'].' '.'00:00:10');
        }
        $data['end_time']=$this->request->param('end_time');   // 中文
        if(!empty($data['end_time'])){
            $data['end_time']=strtotime($data['end_time'].' '.'00:00:10');
        }
        $data['create_time']=time();
        $res=$com->insert_com_dan($data, '', 'active_zsqudao');
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
        }
//        $url=$exhibition['home_like'].'list/register.html?exhibition_id='.$data['exhibition_id'].'&channel_id='.$res.'&type=1';
//        $r = db('active_zsqudao')->where(['id'=>$res])->update(['url'=>$url]);
//        if(!$r){
//            return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
//        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }


    /**
     * 删除
     */
    /**
     * 删除
     */
    public function del(){
        $id = $this->request->param('ids');
        $ids = explode(',',$id);
        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        $msg='';
        $flag=true;
        foreach ($ids as $id){
            $r = db('active_zsqudao')->where(['id'=>$id])->find();
            if(!$r){
                $flag=false;
                $msg='id:'.$id.'记录不存在';
                break;
            }
        }

        if(!$flag){
            return json(['code'=>0,'msg'=>$msg]);
        }

        foreach ($ids as $id){
            $r = db('active_zsqudao')->where(['id'=>$id])->update(['is_delete'=>1]);
            if(!$r){
                $flag=false;
                $msg='id:'.$id.'删除失败请重试';
                break;
            }
        }

        if(!$flag){
            return json(['code'=>0,'msg'=>$msg]);
        }
        return json(['code'=>1,'msg'=>'操作成功']);

    }

    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function edit()
    {
        $com =new CommonPlus();
        // com
        $id = $this->request->param('id');
        $data['exhibition_id'] = $this->request->param('exhibition_id');
        $data['title']=$this->request->param('title');   // 中文
        $data['type']=$this->request->param('type');   // 中文
        $data['number']=$this->request->param('number');   // 中文
        $data['position']=$this->request->param('position');   // 中文
        $data['settlement']=$this->request->param('settlement');   // 中文
        $url=db('exhibition')->where(['id'=>$data['exhibition_id']])->find();
        if(empty($url)){
            return json(['code' => 0, 'msg' => '展会信息错误']);
        }
//        $data['url']=$url['home_like'].'list/register.html?exhibition_id='.$data['exhibition_id'].'&channel_id='.$id.'&type=1';
        $data['update_time']=time();   // 中文
        if(!empty($data['start_time'])){
            $data['start_time']=strtotime($data['start_time'].' '.'00:00:10');
        }
        $data['end_time']=$this->request->param('end_time');   // 中文
        if(!empty($data['end_time'])){
            $data['end_time']=strtotime($data['end_time'].' '.'00:00:10');
        }
        if(empty($data['title'])){
            return json(['code'=>0,'msg'=>'渠道名称有误','data'=>'']);
        }
//        $qdInfo=db('active_zsqudao')->where(['title'=>$data['title'],'exhibition_id'=>$data['exhibition_id']])->find();
//        if($qdInfo){
//            return json(['code'=>0,'msg'=>'渠道名称重复','data'=>'']);
//        }
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '渠道信息有误，请核查']);
        }
        $where['id'] = $id;
        $tbname="active_zsqudao";
        $r=$com->edit_com_fxlist($data, $where, $tbname);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }
        return json(['code' => 1, 'msg' => '操作成功']);
    }


    public function export(){
        $exhibition_id =$this->request->param('exhibition_id');
        $param=" and w.is_delete=0 ";
        if(!empty($exhibition_id)){
            $param.=" and w.exhibition_id=$exhibition_id ";
        }
        $keyword =input('keyword');
        if (!empty($keyword)) {
            $param.=" and w.title like '%$keyword%' ";
        }
        $sql2="select w.*,c.id as exhibition_id,c.title as exhibition_title,p.person_all";
        $sql2.=" from crm_active_zsqudao as w LEFT JOIN crm_exhibition as c ON w.exhibition_id=c.id ";
        $sql2.=" LEFT JOIN (SELECT count(id) as person_all,channel_id FROM crm_zh_active_signup GROUP BY channel_id) AS p ON p.channel_id=w.id ";
        $sql2.=" where 1=1 $param order by w.id desc ";
        $list=Db::query($sql2);
        $columns = array(
            array('title' => 'ID', 'field' => 'id', 'width' => 12),
            array('title' => '活动渠道名称', 'field' => 'title', 'width' => 12),
            array('title' => '所属展会', 'field' => 'exhibition_title', 'width' => 12),
            array('title' => '类型', 'field' => 'type', 'width' => 12),
            array('title' => '编号', 'field' => 'number', 'width' => 12),
            array('title' => '位置', 'field' => 'position', 'width' => 12),
            array('title' => '结算方式', 'field' => 'settlement', 'width' => 12),
            array('title' => '开始推广时间', 'field' => 'start_times', 'width' => 12),
            array('title' => '结束推广时间', 'field' => 'end_times', 'width' => 12),
            array('title' => '登记人数', 'field' => 'person_all', 'width' => 12),
        );
        unset($vv);
        $title = "渠道明细";
        $filename = $title . date('Y-m-d-H-i', time()) . ".xls";
        return json(['code'=>1,'msg'=>'操作成功','data'=>['columns'=>$columns,'filename'=>$filename,'list'=>$list]]);

    }


}
